package com.graduation.service;

import com.graduation.model.ProductEntity;
import com.graduation.mapper.ProductMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 商品操作业务类
 * @Author YaoQi
 * @Date 2018/3/21 23:52
 */
@Service
public class ProductService {

    private static final Logger logger = LoggerFactory.getLogger(ProductService.class);
    @Resource
    private RedisService redisService;

    @Resource
    private ProductMapper productMapper;

    /**
     * @return 返回所以的商品List
     * @Description 获取所有的商品列表
     */
    public List<ProductEntity> getProductList() {

        if (redisService.exists("productList")) {
            List<ProductEntity> productEntityList = (List<ProductEntity>) redisService.get("productList");
            logger.info("getFromRedis, result is" + productEntityList);
            return productEntityList;
        } else {
            List<ProductEntity> productEntityList = productMapper.getProductList();
            if (redisService.set("productList", productEntityList)) {
                logger.info("productList is set to Redis");
            }
            logger.info("getFromDataBase result is " + productEntityList);
            return productEntityList;
        }
    }
}
